﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using System.Web.Security;
using MovingChame.Web.Models;

namespace MovingChame.Web.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AuthenticationService : IAuthenticationService
    {
        public Guid ValidateUser(string username, string password)
        {
            if (Membership.ValidateUser(username, password))
            {
                var user = Membership.GetUser(username);
                return (Guid)user.ProviderUserKey;
            }
            return Guid.Empty;
        }

        public Guid RegisterUser(string username, string password, string email, out string errorMessage)
        {
            errorMessage = string.Empty;
            MembershipCreateStatus status;
            MembershipUser user = Membership.CreateUser(username, password, email, null, null, true, Guid.NewGuid(), out status);
            if (status == MembershipCreateStatus.Success)
                return (Guid)user.ProviderUserKey;
            else
            {
                switch (status)
                {
                    case MembershipCreateStatus.DuplicateEmail:
                        errorMessage = string.Format("The email address {0} is not available.", email);
                        break;
                    case MembershipCreateStatus.DuplicateUserName:
                        errorMessage = string.Format("The user name {0} is not available.", username);
                        break;
                    case MembershipCreateStatus.InvalidEmail:
                        errorMessage = string.Format("The email address {0} is not valid.", email);
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        errorMessage = string.Format("The password is not valid.");
                        break;
                    case MembershipCreateStatus.InvalidUserName:
                        errorMessage = string.Format("The user name {0} is not valid.", username);
                        break;
                    case MembershipCreateStatus.UserRejected:
                        errorMessage = string.Format("The action has been cancelled by the user.");
                        break;
                    default:
                        errorMessage = string.Format("Unknown error.");
                        break;
                }
                return Guid.Empty;
            }
        }

        public bool SendResetPasswordEmail(string email)
        {
            string username = Membership.GetUserNameByEmail(email);
            if (username == null)
                return false;

            var user = Membership.GetUser(username);
            if (user == null)
                return false;

            user.ResetPassword();
            return true;
        }

        public bool ResetPassword(string email, string token, string newPassword)
        {
            string username = Membership.GetUserNameByEmail(email);
            if (username == null)
                return false;

            return Membership.GetUser(username).ChangePassword("<" + token + ">", newPassword);
        }

        public UserData GetUser(Guid providerKey)
        {
            MembershipUser user = Membership.GetUser(providerKey);
            return new UserData() { Username = user.UserName, Email = user.Email };
        }
    }
}
